import time

import one_runtime
import util
from job import sjob, JobCtx
from script.common_storage import node_data_storage


# 每半小时处理一次
@sjob(desc="超1天完成态任务清理", times=["30"])
def clean_complete_task(ctx: JobCtx):
    # node_task
    node_delete = []
    for key, task in one_runtime.tasks.items():
        if task.status != 'complete':
            continue
        monitor = task.monitor
        if task.num == 0 or (monitor and monitor.get('last_count_time') < time.time() - 86000):
            task.status = 'delete'
            node_delete.append(key)

    for key in node_delete:
        del one_runtime.tasks[key]

    # eval_task
    eval_delete = []
    for key, task in one_runtime.eval_tasks.items():
        # 没结束不处理
        if task.status != 'complete':
            continue
        monitor = task.monitor
        # 没有数据处理 或者 监控已经一天没更新了
        if task.num == 0 or (monitor and monitor.get('last_count_time') < time.time() - 86000):
            task.status = 'delete'
            eval_delete.append(key)

    for key in eval_delete:
        del one_runtime.eval_tasks[key]
    return node_delete + eval_delete


@sjob(desc="节点数据集过多清理提醒", times=["06:00"])
def tips_clean_group(ctx: JobCtx):
    tips = {}
    group_map = node_data_storage.distinct_groups()
    for script_name, group_map in group_map.items():
        for node, groups in group_map.items():
            if len(groups) > 30:
                tips['脚本：' + script_name + '， 节点：' + node] = len(groups)

    if not tips:
        return tips
    tip_str = '\n'.join([f'{k}，数据集数量：{v}' for k, v in tips.items()])
    util.tell_dingding({
        "msgtype": "text",
        "text": {
            "content": f'''脚本数据集数量过多通知\n以下节点数据集过多，影响系统性能，建议清理过期数据\n{tip_str}'''
        }
    })
    return tips
